package com.windward.vii.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.windward.vii.common.JsonResult;
import com.windward.vii.common.ResultCode;
import com.windward.vii.dao.entity.UserContent;
import com.windward.vii.service.UserContentService;
import com.windward.vii.service.UserTokenService;
import com.windward.vii.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping(value = "/v1/api1/ugc/content/")
public class UserContentController {

    private static Logger logger = LoggerFactory.getLogger(UserContentController.class);

    @Resource
    private UserContentService userContentService;
    @Resource
    UserTokenService userTokenService;

    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult save(String title, @RequestParam String images,
                           @RequestHeader(value="token") String token) {

        logger.info("save: images={}", images);

        JSONArray jsonArray = JSON.parseArray(images);
        if(jsonArray == null || jsonArray.size() == 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "请选择上传图片，1到9张");
        }
        if(jsonArray.size() > 9) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "上传图片不能超过9张");
        }

        UserContent userContent = new UserContent();
        userContent.setUid(userTokenService.getUidByToken(token));
        userContent.setTitle(title);
        userContent.setImages(images);

        return userContentService.insert(userContent);
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult delete(@RequestParam Long id) {
        if(id <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "请检查参数，ID错误");
        }
        return userContentService.delete(id);
    }


    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult update(@RequestParam Long id, String title, @RequestParam String images) {
        if(id <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "请检查参数，ID错误");
        }
        JSONArray jsonArray = JSON.parseArray(images);
        if(jsonArray == null || jsonArray.size() == 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "请选择上传图片，1到9张");
        }
        if(jsonArray.size() > 9) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "上传图片不能超过9张");
        }

        UserContent userContent = new UserContent();
        userContent.setId(id);
        userContent.setTitle(title);
        userContent.setImages(images);
        return userContentService.update(userContent);
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult list(@RequestParam Long uid, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        logger.info("pageNum={}", pageNum);
        logger.info("pageSize={}", pageSize);
        if(uid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "请检查参数，UID错误");
        }
        return userContentService.getContentListByUid(uid, pageNum, pageSize);
    }
}
